/**
 * @author        hejiyang <silentmanager@163.com>
 * @date          2024-11-19 21:18:31
 * Copyright © YourCompanyName All rights reserved
 */
import { ref } from 'vue'
import { handleTree } from '@/utils/tree' // 假设你的树形处理函数路径
import * as DeptApi from '@/api/system/dept'

export function useDeptList() {
  const deptList = ref<Tree[]>([])
  const loading = ref(false) // 用于控制加载状态
  const error = ref<Error | null>(null) // 存储错误信息

  const fetchDeptList = async () => {
    loading.value = true
    try {
      const data = await DeptApi.getBmsSimpleDeptList()
      deptList.value = handleTree(data)
      error.value = null
    } catch (e) {
      console.error(e)
      error.value = e as Error
    } finally {
      loading.value = false
    }
  }

  // 初始化时自动加载部门列表
  fetchDeptList()

  return {
    deptList,
    loading,
    error,
    fetchDeptList
  }
}
